// export const BASE_URL = 'http://152.136.29.49:9990'
// const API_BASE_URL = 'http://152.136.29.49:9990/api';
export const BASE_URL = 'https://boiler-rating.behold2018.com';
const API_BASE_URL ='https://boiler-rating.behold2018.com/api';

/*判断用户是否登录*/
export const handerLoginStatus = () => {
	const accessToken = uni.getStorageSync('token_key');
	if (!accessToken) {
		uni.showModal({
			title: '登录失效',
			content: '登录失效，请重新登录',
			success: function(res) {
				if (res.confirm) {
					console.log('用户点击确定');
					uni.clearStorage()
					uni.reLaunch({
						url: '/pages/login/login'
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		})
	} else {
		return true
	}
}

export function LoginRequest(options) {
	const {
		url,
		method,
		data
	} = options;
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${API_BASE_URL}${url}`,
			method: method || 'GET',
			data: data,
			header: {
				'Content-Type': 'application/json'
			},
			success: res => {
				if (res.data.success) {
					resolve(res.data.data);
				} else {
					
					uni.showToast({
						title: res.data.msg,
						duration: 1000
					});
					reject(res.data.msg);
				}
			},
			fail: err => {
				reject(err);
			}
		});

	});

}

export function httpRequest(options) {
	const {
		url,
		method,
		data
	} = options;
	const isLogin = handerLoginStatus();

	return new Promise((resolve, reject) => {
		
		if (isLogin) {
			const accessToken = uni.getStorageSync('token_key');
			const header= method=='POST'?{'Content-Type': 'application/json','token': accessToken}:{'token': accessToken}
			uni.request({
				url: `${API_BASE_URL}${url}`,
				method: method || 'GET',
				data: data,
				header: header,
				success: res => {
					
					if (res.statusCode == 200 && res.data.success) {
						resolve(res.data.data);
					} else {
				
						if(res.data.code =='0002' && res.data.msg =='未登录！'){
							uni.showModal({
								title: '登录失效',
								content: '登录失效，请重新登录',
								success: function(res) {
									if (res.confirm) {
										uni.clearStorage()
										uni.reLaunch({
											url: '/pages/login/login'
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							})
						}
						uni.showToast({
							title: res.data.msg,
							duration: 1000
						});
						reject(res.data.msg);
					}
				},
				fail: err => {
					reject(err);
				}
			});
		}

	});

}

/**上传图片**/
export function uploadRequest(options){
	const {
		url,
		filePath,
		data
	} = options;
	
	return new Promise((resolve, reject) => {
		const accessToken = uni.getStorageSync('token_key');
		uni.uploadFile({
			url: `${API_BASE_URL}${url}`,
			filePath: filePath,
			name: 'file',
			formData:data,
			header:{
				'Content-Type': 'multipart/form-data',
				'token': accessToken
			},
			success: (res) => {
				const data=JSON.parse(res.data);
				if(data.success){
					resolve(data.data);
				}else {
					uni.showToast({
						title: data.msg,
						duration: 1000
					});
					reject(data.msg);
				}
				
				
			},
			fail:(error)=>{
				console.error(error)
			}
		});
	})
}